package com.erp.module.caiLiaoZhiLiang

import android.content.Context
import android.util.Log
import com.erp.bean.CaiLiaoZhiLiangBean
import com.erp.contacts.Contacts
import com.erp.contacts.Contacts.BOUNDARY
import com.erp.Interface.OnUserDataInterface
import com.erp.util.Httputil.*
import com.erp.util.Myutil.dateToStamp
import org.xutils.http.RequestParams
import org.xutils.x
import java.io.File


/**
 * Created by Administrator on 2019/3/11/011.
 * 作者：刘洋
 */
class CaiLiaoZhiLiangModel : CaiLiaoZhiLiangContract.CaiLiaoZhiLiangMP {
    override fun setGongyingCailiaoZhiliang(context: Context, code: Int, bean: CaiLiaoZhiLiangBean, deletephoto: List<CaiLiaoZhiLiangBean.photo>, onUserDataInterface: OnUserDataInterface) {
        var params: RequestParams = if (code == 1) {
            RequestParams(Contacts.CAILIAO_ZHILIANG)
        } else {//code  = 2
            RequestParams(Contacts.CAILIAO_ZHILIANG + "/" + bean.id + "/edit")
        }
        getparams(context, params)
        addtoken(params)
        setProsess(params)
        params.addHeader("ACCEPT", "application/json")
        Log.d("TAAAG", bean.toString())
        params.addBodyParameter("iu_id", bean.iu_id, BOUNDARY)
        params.addBodyParameter("gi_id", bean.gi_id, BOUNDARY)
        params.addBodyParameter("mc_2_id", bean.mc_2_id, BOUNDARY)
        params.addBodyParameter("qi_id", bean.qi_id, BOUNDARY)
        params.addBodyParameter("test_time", dateToStamp(bean.jianyan_time), BOUNDARY)
        params.addBodyParameter("cailiao_shuliang", bean.cailiao_shuliang, BOUNDARY)
        params.addBodyParameter("choucha_shuliang", bean.choucha_shuliang, BOUNDARY)
        params.addBodyParameter("hege_shuliang", bean.hege_shuliang, BOUNDARY)
        params.addBodyParameter("quality_decision", bean.quality_decision, BOUNDARY)
        params.addBodyParameter("process_mode", bean.process_mode, BOUNDARY)
        params.addBodyParameter("solution", bean.solution, BOUNDARY)
        params.addBodyParameter("description", bean.description, BOUNDARY)
        for (i in bean.listphoto.indices) {
            val photo = bean.listphoto[i]
            if (photo.fid == "") {
                params.addBodyParameter("files[new][$i]", File(photo.uri), BOUNDARY)
            }
        }
        for (i in deletephoto.indices) {
            if (deletephoto[i].fid != "") {
                params.addBodyParameter("files[delete][" + deletephoto[i].fid + "]", deletephoto[i].fid, BOUNDARY)
            }
        }

        x.http().post(params, Call(context, onUserDataInterface))
    }

    override fun getWentiDaima(context: Context, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.GONGYING_ZHILIANG_DAIMA)
        getparams(context, params)
        x.http().get(params, Call(context, onUserDataInterface))
    }

    override fun getCailiaoLeibie(context: Context, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.CAILIAO_LEIBIE)
        getparams(context, params)
        x.http().get(params, Call(context, onUserDataInterface))
    }

    override fun getGongyingshangLieBiao(context: Context, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.GONGYING_LIEBIAO)
        getparams(context, params)
        x.http().get(params, Call(context, onUserDataInterface))
    }

    override fun getHuopingBianma(context: Context, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.HUOPING_BIANMA)
        getparams(context, params)
        x.http().get(params, Call(context, onUserDataInterface))
    }

    override fun getGongyingCailiaoZhiliang(context: Context, page: Int, pagesize: Int, sousuo: String,hege :String, startTime: String, endTime: String, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.CAILIAO_ZHILIANG)
        getparams(context, params)
        setProsess(params)
        params.addBodyParameter("page", page.toString())
        params.addBodyParameter("pagesize",pagesize.toString())
        params.addBodyParameter("searchParams[iucode_giname]", sousuo)
        params.addBodyParameter("searchParams[smq_hege]", hege)
        params.addBodyParameter("searchParams[time_filter][time_category]", "smq.test_time")
        params.addBodyParameter("searchParams[time_filter][start_time]", startTime)
        params.addBodyParameter("searchParams[time_filter][end_time]", endTime)
        x.http().get(params, Call(context, onUserDataInterface))
    }
}